package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.StgyException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.PaymentType;
import com.atguigu.lease.web.admin.service.PaymentTypeService;
import com.atguigu.lease.web.admin.mapper.PaymentTypeMapper;
import org.springframework.stereotype.Service;

/**
 * @author gqs
 * @description 针对表【payment_type(支付方式表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class PaymentTypeServiceImpl extends ServiceImpl<PaymentTypeMapper, PaymentType>
    implements PaymentTypeService {

  @Override
  public boolean saveOrUpdatePaymentType(PaymentType paymentType) {
    // 获取传入传入参数的id 如果为空返回一个不存在的值
    Long id = paymentType.getId() == null ? -1 : paymentType.getId();

    // 先根据传入name 查询是否有符合条件的数据
    PaymentType dbPaymentType =
        this.getOne(
            Wrappers.<PaymentType>lambdaQuery().eq(PaymentType::getName, paymentType.getName()));

    if (dbPaymentType != null && !id.equals(dbPaymentType.getId())) {
      // 通过异常对象携带 失败的code值和message
        throw new StgyException(ResultCodeEnum.PAYMENT_TYPE_EXISTS_ERROR);
    }
    // 如果没有数据，或者数据id与传入的id一致，则直接保存
    return this.saveOrUpdate(paymentType);
  }
}
